// pages/index/calcDetail/calcDetail.js
const app = getApp();
var date = require('../../util/date.js');
var util = require('../../util/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail:{},
    tocalc: {},
    nickName: null,
    avatarUrl: null,
    isFast: false,
    fromCalc: false, 
  },
  //快速导航的点击
  tapIn(e) {
    let index = e.currentTarget.dataset.index;
    if (index == "1") {
      //收起
      this.setData({
        isFast: false
      })
    } else {
      //展开
      this.setData({
        isFast: true
      })
    }
  },
  //获取详情
  getDetail(id) {
    const that = this;
    const url = app.globalData.api + "/wechat/calculation/get/" + id;
    wx.request({
      url: url,
      method: "get",
      success: function (res) {
        let data = res.data;
        if (data.errCode == 0) {
          let item = data.data.calculation;
          let user = data.data.user;
          //处理到期日期
          if (!item.expirationDate) {
            item.expiringDate = ""
            item.eWeekDay = ""
          } else {
            item.expiringDate = date.formatDate(item.expirationDate, 'yyyy-MM-dd');
            item.eWeekDay = util.weekDay(item.expirationDate)
          }
          //处理计算日期
          if (!item.createTime) {
            item.calcTime = ""
          } else {
            item.calcTime = date.formatDate(item.createTime, 'yyyy-MM-dd hh:mm');
          }
          //处理贴息日期
          if (!item.discountDate) {
            item.oDiscountDate = ""
            item.dWeekDay = ""
          } else {
            item.oDiscountDate = date.formatDate(item.discountDate, 'yyyy-MM-dd');
            item.dWeekDay = util.weekDay(item.discountDate)
          }
          //处理贴现利息、贴现金额 每十万扣款 （设置千分符）
          if (item.debit10){
            item.oDebit10 = util.comdify(item.debit10);//每十万扣款
          }else{
            item.oDebit10 = 0;//每十万扣款
          }
          if (item.discountAmount){
            item.oDiscountAmount = util.comdify(item.discountAmount);//贴现金额
          }else{
            item.oDiscountAmount = 0
          }
          if (item.discountRate){
            item.oDiscountRate = util.comdify(item.discountRate);//贴现利息
          }else{
            item.oDiscountRate = 0 
          }
          //用户头像和昵称
          item.headPortrait = user.headPortrait;
          item.userName = user.userName;
          app.globalData.calcContent.fromCalc=false
          that.setData({
            fromCalc: false,
            detail: item,
            tocalc: {
              billType: item.billType,
              currentDate: item.oDiscountDate,
              expiringDate: item.expiringDate,
              changeDays: item.daysRemaining,
              days: item.interestDay,
              billAmount: item.billAmount,
              yearRate: util.dot(item.interestRateYear),
              monthRate: util.dot(item.monthlyInterestRate),
              serviceCharge: util.dot(item.serviceCharge),
              discountInterest: util.dot(item.oDiscountRate),
              discountAmount: util.dot(item.oDiscountAmount),
              debit10: util.dot(item.oDebit10),
              isBillDetail: false,
              isCalcDetail: true
            }
          })
        } else {
          wx.showToast({
            title: res.data.errMsg,
            icon: "none",
          })
        }
      },
      fail: function (e) {
        wx.showToast({
          title: "网络错误，请稍后重试！",
          icon: "none",
        })
      }
    })
  },
  //点击计算，跳转计算器
  toCalc() {
    var data1 = this.data;
    //传参 ： 剩余天数，到期日期 （贴现日期是当天）金额 年利率 手续费 每十万扣款 
    app.globalData.calcParameters = data1.tocalc;
    wx.switchTab({
      url: '/pages/index/index'
    })
  }, 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //用户昵称和头像前台自获取
    let user = app.globalData.userInfo
    this.setData({
      nickName: user.nickName,
      avatarUrl: user.avatarUrl,
    })
    // console.info("历史计算详情id=" + options.id);
    if (!options.id || options.id == "undefined") {
      wx.showToast({
        title: '当前网络异常，请稍后重试',
        icon: "none"
      })
      return
    }
    this.getDetail(options.id); 
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})